package phapp.service.data.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;
import phapp.model.custom.Custom;
import phapp.model.custom.CustomFavorite;
import phapp.model.data.*;
import phapp.dao.data.*;
import phapp.service.data.impl.*;
import phapp.service.data.*;

/**
 * Title: t_qfdtext_data 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("qfdtextManager")
@Transactional
public class QfdtextManagerImpl extends BaseManager<Qfdtext, java.lang.Integer> implements QfdtextManager
{

   /** 定义Dao */
   private QfdtextDao qfdtextDao;

   /** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
   public void setQfdtextDao(QfdtextDao dao)
   {
      this.qfdtextDao = dao;
   }

   /** 获取Dao */
   public EntityDao getEntityDao()
   {
      return this.qfdtextDao;
   }

   /**
    * <p>
    * 分页查询Qfdtext<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return qfdtextDao.findByPageRequest(pr);
   }

   /**
    * <p>
    * 批量删除 Qfdtext<br>
    * 
    * @param Qfdtext
    *           u
    * @return Boolean
    * @author
    */
   public Boolean removeByIdArry(Qfdtext u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = qfdtextDao.removeByIdArry(u);
      if(i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }

   public List findByDayLevel(Map map)
   {
      return qfdtextDao.findByDayLevel(map);
   }

   public Map getQfwordData(JSONObject jsonData)
   {
      String strType = "";
      int nStatus = 0, i = 0;
      JSONArray arrayData = null;
      JSONObject jsonQfdtext = null;
      Qfdtext qfdtextData = null;
      List<Qfdtext> listData = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         strType = jsonData.getString("qfwordtype");
         if(strType.compareTo("全部") == 0)
            strType = "";
         
         mapQuery.clear();
         mapQuery.put("textType", 2);
         if(strType != null && strType.length() > 0)
            mapQuery.put("textSubtype", strType);
         //mapQuery.put("textSubtype", 2);
         listData = qfdtextDao.findList(mapQuery);

         arrayData = new JSONArray();
         for(i = 0; i < listData.size(); i++)
         {
            qfdtextData = listData.get(i);
            jsonQfdtext = new JSONObject();
            jsonQfdtext.put("qfwordtype", qfdtextData.getTextSubtype());
            jsonQfdtext.put("qfword", qfdtextData.getTextContent());

            arrayData.add(jsonQfdtext);
            jsonQfdtext = null;
         }

         mapResult.put("qfwordlist", arrayData);
         nStatus = 1000;
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000) mapResult.put("message", "获取祈福语成功！");
         else mapResult.put("message", "获取祈福语失败！");
      }

      return mapResult;
   }

}
